var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var point_map = require("./pointMap");

var treasure = {
  go_page_from_home() {
    var logger = utils.create_logger("treasure go_page_from_home: ");
    utils.click_point.apply(
      utils,
      utils.get_size_point(point_map["首页我的"], 1080, 2340)
    );
    sleep(utils.get_random_interval(2, 0, 1, 1000));
    var ocr_text = ocr_utils.screen_exists_text_one(["去赚现金", "点我领钱"]);
    if (ocr_text) {
      console.log("找到-去赚现金或点我领钱");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(4, 0, 1, 1000));
      var interval = utils.get_random_interval(6, 0, 2, 1000);
      logger("等待--" + interval / 1000 + "--秒后开始判断是否开宝箱");
      sleep(interval);
      this.init();
    } else {
      console.log("没找到-去赚现金或点我领钱");
    }
  },
  init() {
    console.log("treasure init");
    var ocr_text = ocr_utils.screen_exists_text_one("点击领金币", true);
    if (ocr_text) {
      console.log("treasure go_page_from_home: 找到--点击领金币--");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var ocr_text = ocr_utils.screen_exists_text_one("看广告视频再得");
      if (ocr_text) {
        console.log("treasure init: 找到--看广告视频再得--");
        utils.click_ocr_text(ocr_text);
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        this.run();
      } else {
        console.log("treasure init: 没找到--看广告视频再得--");
        back();
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        this.after();
      }
    } else {
      console.log("treasure go_page_from_home: 没找到--点击领金币--");
      this.after();
    }
  },
  run() {
    ad.has_watch_ad_times = 0;
    ad.init();
    this.after();
  },
  after() {
    console.log("treasure after");
    if (utils.click_text_starts_with("开心收下")) {
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    }
    // this.next();
  },
  next() {
    console.log("treasure next");
  },
};
module.exports = treasure;
